package com.ellecity06.debugtools.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import com.ellecity06.debugtools.databinding.ActivityDebugBaseBinding
import com.gyf.immersionbar.ktx.immersionBar
import com.gyf.immersionbar.ktx.statusBarHeight
import java.lang.reflect.ParameterizedType

/**
 * @author ellecity06
 * @time 2023/3/29 15:06
 * @des mvvm版的基类
 */
abstract class BaseDebugActivity<out VB : ViewBinding, out VM : BaseDebugViewModel> : AppCompatActivity() {

    protected val viewModel: VM by lazy { initViewModel() }
    protected val binding: VB by lazy { inflateBinding() }
    protected val baseBinding: ActivityDebugBaseBinding by lazy { ActivityDebugBaseBinding.inflate(layoutInflater) }
    abstract fun inflateBinding(): VB

    /**
     * 初始话 --onCrete
     */
    abstract fun initialize(savedInstanceState: Bundle?)

    /**
     * 配置项是否使用base的title
     */
    open val userBaseTitle = true

    /**
     * 初始化数据，网络请求数据初始化需要复写这个函数
     */

    open fun initData() = Unit
    open fun initObserver() = Unit
    open fun initEvent() = Unit

    /**
     * ViewModel的订阅
     */
    open fun onViewEffectObserver(effect: ViewEffect) = Unit
    override fun onCreate(savedInstanceState: Bundle?) {
        // 设置固定竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        super.onCreate(savedInstanceState)
        if (userBaseTitle) {
            baseBinding.layoutContent.addView(binding.root)
            val layoutParams = baseBinding.statusView.layoutParams
            layoutParams.height = statusBarHeight
            baseBinding.statusView.layoutParams = layoutParams
            setContentView(baseBinding.root)
        } else {
            setContentView(binding.root)
        }
        immersionBar {
            statusBarDarkFont(true,0.5f)
        }
        initBaseObserver()
        initBaseEvent()
        initObserver()
        initEvent()
        initialize(savedInstanceState)
        lifecycleScope.launchWhenCreated {
            initData()
        }


    }

    private fun initBaseEvent() {
        if (userBaseTitle) {
            baseBinding.ivBack.setOnClickListener {
                onBackPressed()
            }
        }
    }


    private fun initBaseObserver() {
        lifecycleScope.launchWhenCreated {
            viewModel.viewEffect.collect { effect ->
                onViewEffectObserver(effect)
            }
        }
    }


    open fun initViewModel(): VM {
        val type = javaClass.genericSuperclass
        return if (type is ParameterizedType) {
            val tp = type.actualTypeArguments[1]
            val tClass = tp as? Class<VM> ?: BaseDebugViewModel::class.java
            ViewModelProvider(this)[tClass] as VM
        } else {
            viewModels<BaseDebugViewModel>().value as VM

        }

    }

    /**
     * 隐藏返回按钮
     */
    protected fun hideBackIcon() {
        if (userBaseTitle) baseBinding.ivBack.visibility = View.GONE
    }

    protected fun setTitle(title: String) {
        if (userBaseTitle) baseBinding.tvTitle.text = title
    }

}